<template>
  <div>
    <BaseForm
      ref="searchForm"
      type="search"
      :items="formItems"
      v-model:form="searchParams"
      labelWidth="120px"
      @search="handleSearch"
    >
      <template #footer>
        <div style="padding-bottom: 10px">
          已启用停电信息审核的微信群：<span style="color: #f59a23">{{
            totalEnable
          }}</span>
        </div>
      </template>
    </BaseForm>
    <BaseTable
      ref="baseTable"
      :searchParams="searchParams"
      :getTableDataFn="basicConfigService.pageListWxGroupResDto"
      :select="true"
      :handleDisable="handleDisable"
    >
      <template #select>
        <el-table-column type="selection" width="55" />
      </template>
      <el-table-column prop="groupId" label="微信群ID" width="120" />
      <el-table-column
        prop="wxGroupName"
        label="微信群名称"
        minminWidth="150"
      />
      <el-table-column
        prop="communityDtoList"
        label="相关片区/村社"
        minminWidth="200"
      >
        <template #default="scope">
          <Tooltip
            :text="
              scope.row.communityDtoList
                ?.map((e) => e.communityName)
                ?.join('、')
            "
          ></Tooltip>
        </template>
      </el-table-column>
      <el-table-column
        prop="managerDtoList"
        label="关联网格经理"
        minminWidth="200"
      >
        <template #default="scope">
          <Tooltip
            :text="
              scope.row.managerDtoList?.map((e) => e.managerName)?.join('、')
            "
          ></Tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="statusName" label="停电信息审核" minminWidth="120">
        <template #default="scope">
          <isDisableStatus :status="scope.row.isPowerAudit"></isDisableStatus>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="70">
        <template #default="scope">
          <el-link
            :underline="false"
            link
            type="primary"
            size="small"
            @click.prevent="handleDisable(scope.row)"
            >{{ scope.row.isPowerAudit == 1 ? '启用' : '停用' }}</el-link
          >
        </template>
      </el-table-column>
    </BaseTable>
  </div>
</template>

<script setup lang="ts">
import openMsgBox from '@/hooks/messageBox.js'
import { basicConfigService } from '@/api/basicConfig'
const options = ref([])
const formItems = ref([
  {
    name: '微信群名称',
    key: 'wxGroupName',
    type: 'input',
  },
  {
    name: '相关片区/村社',
    key: 'communityName',
    type: 'input',
  },
  {
    name: '关联网格经理',
    key: 'managerId',
    type: 'select',
    options: options,
  },
  {
    name: '停电信息审核',
    key: 'isPowerAudit',
    type: 'select',
    options: [
      { label: '启用', value: 0 },
      { label: '停用', value: 1 },
    ],
  },
])
const getAllManager = () => {
  basicConfigService.getAllManager({ data: {} }).then((res) => {
    let data = res.data.map((e) => {
      return {
        value: e.managerId,
        label: e.managerName,
      }
    })
    options.value.push(...data)
  })
}
const searchParams = ref({
  planCateId: '',
  schemeId: '',
  date: '',
})
const totalEnable = ref(0)
const baseTable = ref(null)
const handleSearch = () => {
  baseTable.value.handleSearch(searchParams.value)
  getTotalEnable()
}
const handleDisable = (row, rows = []) => {
  let isPowerAudit = row.isPowerAudit || row
  let ids = row.groupId ? [row.groupId] : rows.map((e) => e.groupId)

  openMsgBox(
    {
      tips: `是否确定${isPowerAudit == 1 ? '启用' : '停用'}?`,
    },
    () => {
      if (isPowerAudit == 1) {
        return basicConfigService.powerEnableBatch({
          data: ids,
        })
      } else {
        return basicConfigService.powerDisableBatch({
          data: ids,
        })
      }
    },
    () => {
      handleSearch()
    }
  )
}
const getTotalEnable = () => {
  basicConfigService
    .pageListWxGroupResDto({ data: { ...searchParams.value, isPowerAudit: 0 } })
    .then((res) => {
      totalEnable.value = res.data.total
    })
}
onMounted(() => {
  getAllManager()
  getTotalEnable()
})
</script>

<style lang="less" scoped></style>
